<?php namespace Zders\Customtools\FormWidgets;

use Backend\Classes\FormWidgetBase;
use Zders\Customtools\Models\Settings;

/**
 * BaiduMapLocation Form Widget
 */
class BaiduMapLocation extends FormWidgetBase
{
    /**
     * @inheritDoc
     */
    protected $defaultAlias = 'map_location';

    /**
     * @inheritDoc
     */
    public function init()
    {
    }

    /**
     * @inheritDoc
     */
    public function render()
    {
        $this->prepareVars();
        return $this->makePartial('baidumaplocation');
    }

    /**
     * Prepares the form widget view data
     */
    public function prepareVars()
    {
        $defaultPoint = Settings::get('default_postion');
        $this->vars['is_config'] = empty(Settings::get('map_ak'))? false : true;
        $this->vars['name'] = $this->formField->getName();
        $this->vars['value'] = empty($this->getLoadValue())?$defaultPoint:$this->getLoadValue();
        $this->vars['model'] = $this->model;
    }

    /**
     * @inheritDoc
     */
    public function loadAssets()
    {
        $baseUrl = Settings::get('map_url');
        $ak = Settings::get('map_ak');
        $v = Settings::get('version');
        $type = Settings::get('type');
        $map_url = $baseUrl.'?type='.$type.'&v='.$v.'&ak='.$ak;
        $defaultPoint = Settings::get('default_postion');
        $attrbuiter = [
            'sync',
            'defer',
            'id'=>'map_config',
            'point_value'=>empty($this->getLoadValue())?$defaultPoint:$this->getLoadValue(),
            'input_id'=>$this->getId('input')
        ];
        $this->addJs($map_url, $attrbuiter);

        $this->addJs('js/baidumaplocation.js', ['defer']);
        $this->addCss('css/baidumaplocation.css', 'zders.customtools');
    }

    /**
     * @inheritDoc
     */
    public function getSaveValue($value)
    {
        return $value;
    }
}
